package com.wunu.smartlock1.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockManageApi;
import com.wunu.smartlock1.net.response.ApiNormalResponse;
import com.wunu.smartlock1.net.response.ApiSimpleResponse;
import com.wunu.smartlock1.net.response.LockQrCode;
import com.wunu.smartlock1.util.CommonUtil;
import com.wunu.smartlock1.util.ImageLoaderPicture;

import java.lang.ref.WeakReference;

import retrofit2.Call;


/**
 * 二维码
 * Created by admin on 2018/4/21.
 */

public class QrcodeActivity extends AppCompatActivity{
    LockManageApi service = NetComponent.getRetrofit().create(LockManageApi.class);
    String toast;

    ImageView pic;//锁二维码
    ImageView qrcode;//锁二维码

    TextView username;//
    private ProgressDialog pd;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+msg);
            //只要执行到这里就关闭对话框
            pd.dismiss();

            System.out.println("obj->"+msg.getData());
            LockQrCode qrCodeData = ( LockQrCode)msg.obj;
            System.out.println("obj:"+qrCodeData);

            if (qrCodeData == null){
                Toast.makeText(QrcodeActivity.this, toast+"获取失败"+qrCodeData, Toast.LENGTH_SHORT).show();
            }

            username.setText(qrCodeData.getName());
            ImageLoader.getInstance().displayImage(qrCodeData.getAvatar(), pic,new ImageLoaderPicture(QrcodeActivity.this).getOptions(),new SimpleImageLoadingListener());
            ImageLoader.getInstance().displayImage(qrCodeData.getQrCodeUrl(), qrcode,new ImageLoaderPicture(QrcodeActivity.this).getOptions(),new SimpleImageLoadingListener());
            Toast.makeText(QrcodeActivity.this, toast+"获取成功"+qrCodeData, Toast.LENGTH_SHORT).show();
        }
    };
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_qrcode);
        pic = findViewById(R.id.pic);
        qrcode = findViewById(R.id.qrcode);
        username = findViewById(R.id.username);

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Call<ApiNormalResponse<LockQrCode>> call = service.getLockQrCode(3);
                    ApiNormalResponse<LockQrCode> resp = call.execute().body();
                    System.out.println(resp.toString());
                    // 发送消息，把服务器返回的本文弹出吐司显示
                    Message msg = handler.obtainMessage();
                    if(resp == null || resp.getData() == null)msg.obj = null;
                    else msg.obj = resp.getData().getData();
                    toast = "锁二维码";
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    System.out.println("二维码异常:" + e);
                }
            }
        };
        //构建一个下载进度条
        pd= ProgressDialog.show(QrcodeActivity.this, "请等待", "正在获取信息…");
        thread.start();
    }
}
